function updatePercent() {
    var n = $(".question-set");
    doneWithQ = !0,
    n.each(function() {
        var u = $(this).find("li.question-item"),
        f = u.length,
        i = 0,
        t,
        r;
        u.each(function() {
            var r = $(this).find("textarea"),
            u = $(this).find("input:text"),
            f = $(this).find("input:checked"),
            t = $(this).find("select"),
            e = $(this).find("li");
            r.length > 0 && r.val() != "" && i++,
            u.length > 0 && u.val() != "" && i++,
            f.length > 0 && i++,
            t.length > 0 && t.val() != "-- Choose Answer --" && t.val() != "" && i++,
            e.length > 0 && i++
        }),
        t = i / f * 100 + "%",
        t == "0%" && (t = "5%"),
        r = $(this).prev("h2").find("em"),
        r.css("width", t).html(t),
        r.attr("title", t),
        t == "100%" ? r.css("background-color", "#d5edb3") : doneWithQ = !1
    })
}
function uploadDone() {
    osmosis.actionState({
        Succeed: !0,
        Message: ""
    });
    var n = document.getElementById("upload_target").contentWindow.document.body.innerHTML;
    $("#uploaded-files-list").append(n)
}
$(function() {
    osmosis.init(),
    $(".accordion").accordion({
        autoHeight: !1
    }),
    $(".datepicker").datepicker();
    var n = document.getElementById("file_upload_form");
    n != null && (n.onsubmit = function() {
        osmosis.spin(),
        document.getElementById("file_upload_form").target = "upload_target"
    }),
    $("#upload_target").load(function() {
        uploadDone()
    }),
    $(".answer_autosave").live("change", 
    function() {
        var r;
        $(".saving").addClass("show");
        var i = $(this).parents("li"),
        f = $("#client").data("id"),
        e = i.data("s"),
        o = i.data("q"),
        t = "",
        u = i.find(".answer");
        if (u.length > 0 ? t = u.val() : (r = i.find(":checked"), r.length == 1 ? t = r.val() : r.each(function(n) {
            n == 0 ? t = $(this).val() : t += "~||~" + $(this).val()
        })), autosave == !1) {
            $(".saving").removeClass("show"),
            updatePercent();
            return
        }
        $.post("/q/autosave", {
            id: f,
            sectionId: e,
            questionId: o,
            answer: t
        },
        function(n) {
            $(".saving").removeClass("show"),
            n.Succeed ? (updatePercent(), $("#saved").addClass("show"), setTimeout(function() {
                $("#saved").removeClass("show")
            },
            3e3)) : (osmosis.actionState(n), osmosis.notify("Your last changes was not saved. You may contact us so we can diagnose what' happenning.", !0))
        })
    }),
    $(".add-links").click(function(n) {
        n.preventDefault(),
        osmosis.spin();
        var r = $(this).data("qnid"),
        i = $(this).data("qid"),
        t = new Modal;
        $.post("/q/preparelink", {},
        function(n) {
            osmosis.stopSpin(),
            t.init(n, "Add", "Cancel", 
            function() {
                $("#inspiration-url").removeClass("errors");
                var n = $("#inspiration-url").val(),
                u = $("#inspiration-desc").val();
                if (n == "") {
                    $("#inspiration-url").addClass("errors").focus();
                    return
                }
                t.finalize(),
                osmosis.spin(),
                $.post("/q/addlink", {
                    id: r,
                    questionId: i,
                    url: n,
                    desc: u
                },
                function(n) {
                    n == !1 ? osmosis.actionState({
                        Succeed: !1,
                        Message: "Unable to add the link."
                    }) : (osmosis.actionState({
                        Succeed: !0,
                        Message: ""
                    }), $("#il-" + i).append(n))
                })
            },
            function() {
                t.finalize()
            })
        })
    });
    $(".question-item").on({
        click: function(n) {
            n.preventDefault(),
            osmosis.spin();
            var t = $(this).parent(),
            i = $(this).data("id"),
            r = $(this).data("qnid"),
            u = $(this).data("qid");
            $.post("/q/deletelink", {
                id: i,
                qnId: r,
                qId: u
            },
            function(n) {
                osmosis.actionState(n),
                n.Succeed ? t.remove() : osmosis.notify("We were not able to delete this link.", !0)
            })
        }
    },
    ".delete-links");
    $(".question-item").on({
        click: function(n) {
            n.preventDefault(),
            osmosis.spin();
            var t = $(this).parent(),
            i = $(this).data("id"),
            r = $(this).data("qnid"),
            u = $(this).data("qid"),
            f = $(this).data("file");
            $.post("/q/deletefile", {
                id: i,
                qnId: r,
                qId: u,
                filename: f
            },
            function(n) {
                osmosis.actionState(n),
                n.Succeed ? t.remove() : osmosis.notify("We were not able to delete this file.", !0)
            })
        }
    },
    ".delete-files");
    $("#completed-q").click(function(n) {
        n.preventDefault();
        var t = $(this).parent();
        osmosis.spin(),
        $.post("/q/completed", {
            id: $("#client").data("id"),
            aId: $("#client").data("aid")
        },
        function(n) {
            osmosis.actionState(n),
            n.Succeed ? t.html("Thank you! The questionnaire is now completed.") : (osmosis.notify("An error occured while processing the questionnaire.<br />We will save the current state so no data will be lost.", !0), osmosis.spin(), $.post("/q/savestate", {
                id: $("#client").data("id"),
                data: $("#client").html()
            },
            function(n) {
                osmosis.actionState(n),
                n.Succeed || t.html("Something really bad happend.<br /><br />You might need to print each section so you do not loose any data.")
            }))
        })
    }),
    $(".unlink-question").click(function(n) {
        n.preventDefault(),
        osmosis.spin();
        var t = $(this).parents("li"),
        i = $("#client").data("id"),
        r = $(this).data("s"),
        u = $(this).data("q");
        $.post("/q/unlink", {
            id: i,
            sectionId: r,
            questionId: u
        },
        function(n) {
            osmosis.actionState(n),
            n.Succeed ? t.remove() : osmosis.notify("Hmm, that question could not be removed.<br />One workaround while we investigate would be to remove the question from the section itself.", !0)
        })
    })
})